package com.huazhi.wzsnw.service.impl;

import com.huazhi.wzsnw.mapper.CircuitMapper;
import com.huazhi.wzsnw.mapper.XzMapper;
import com.huazhi.wzsnw.model.Circuit;
import com.huazhi.wzsnw.model.CurcuitTj;
import com.huazhi.wzsnw.model.Xzzd;
import com.huazhi.wzsnw.service.CircuitService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CircuitServiceImpl implements CircuitService {

    @Autowired
    private CircuitMapper circuitMapper;
    @Autowired
    private XzMapper xzMapper;

    @Override
    public Circuit selectByPrimaryKey(String tid) {
        return circuitMapper.selectByPrimaryKey(tid);
    }

    @Override
    public int insert(Circuit record) {
        return circuitMapper.insert(record);
    }

    @Override
    public List<String> getCircuitList(String key, Circuit circuit) {
        return circuitMapper.getCircuitList(key, circuit);
    }

    @Override
    public int delete(Circuit record) {
        return circuitMapper.deleteByPrimaryKey(record.getTid());
    }

    @Override
    public int deletes(List<Circuit> records) {
        if (!records.isEmpty()) {
            for (Circuit circuit : records) {
                delete(circuit);
            }
        }
        return 0;
    }
    @Override
    public int update (Circuit record){return circuitMapper.updateByPrimaryKey(record);}
    @Override
    public Circuit findByTid (String tid){return circuitMapper.selectByPrimaryKey(tid);}
//    @Override
//    public List<String> selectCircuitList(String key,Circuit circuit){return circuitMapper.getCircuitList(key,circuit);}
    @Override
    public List<Circuit> getListCircuitByListTid(List<String> stringList, Integer pageIndex, Integer pageSize){
        return circuitMapper.selectListCircuitByListTid(stringList,pageIndex,pageSize);}
    @Override
    public JSONObject getCircuitListOne(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        int pageSize = Integer.parseInt(request.getParameter("limit"));
        int pages = Integer.parseInt(request.getParameter("page"));
        Integer pageIndex = (pages - 1) * pageSize;
        List<String> list = getSearchCircuitList(request);
        if(list.isEmpty()){
            jsonObject.put("data", false);
            jsonObject.put("count", 0);
            jsonObject.put("msg", "");
        }else{
            List<Circuit> circuitList = getListCircuitByListTid(list,pageIndex,pageSize);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            for (Circuit c: circuitList) {
                if(null != c.getSbwxrq()){
                    c.setSbwxrqString(simpleDateFormat.format(c.getSbwxrq()));
                }
                if(null != c.getXcrq()){
                    c.setXcrqString(simpleDateFormat.format(c.getXcrq()));
                }
                if(null != c.getSbwxrq() && !"".equals(c.getSbwxry())){
                   c.setSfwx("是");
                }else {
                    c.setSfwx("否");
                }
            }
            jsonObject.put("data", circuitList);
            jsonObject.put("count", list.size());
            jsonObject.put("msg", "");
        }
        jsonObject.put("code", 0);
        return jsonObject;
    }

    @Override
    public List<String> getSearchCircuitList(HttpServletRequest request){
        String search = request.getParameter("key");
        String selectdata = request.getParameter("selectdata");
        List<String> list = new ArrayList<>();
        if(selectdata != null && !selectdata.isEmpty()){
            String[] ids = selectdata.split(",");
            if(ids.length >0 ){
                list.addAll(Arrays.asList(ids));
            }
        }else {
            Circuit circuit =new Circuit();
            List<String> params = new ArrayList<>();
            if ("".equals(search) || search == null) {
                params.add("");
            } else if (search.contains(" ")) {
                params = Arrays.asList(search.split(" "));
            } else if (!search.contains(" ")) {
                params.add(search);
            }
            boolean firstSearch = true;
            for (String oneParam : params) {
                if (firstSearch) {
                    list = getCircuitList(oneParam,circuit);
                    firstSearch = false;
                } else {
                    List<String> middleList = getCircuitList(oneParam,circuit);
                    list.retainAll(middleList);
                }
            }
        }
        return list;
    }

    @Override
    public List<Date> selectXcDate(String zrc) {
       return circuitMapper.selectXcDate(zrc);
    }

    @Override
    public List<Circuit> seleByXcrq(String xcrq) {
        return  circuitMapper.seleByXcrq(xcrq);
    }

    @Override
    public List<CurcuitTj> sleletongji(Circuit circuit) {
        List<Circuit>  list = circuitMapper.sleletongji(circuit);
        List<String> xzNameList = new ArrayList<>();
        Map<String,List<String>> cwhMap = new HashMap<>();
        List<CurcuitTj> curcuitTjs = new ArrayList<>();
        for (Circuit c :list) {
            if(c.getXz()!= null&& !"".equals(c.getXz())){
               if(!checlArrayHave(c.getXz(),xzNameList)){
                   xzNameList.add(c.getXz());
               }
                if(c.getCwh()!= null &&! "".equals(c.getCwh())){
                    if(cwhMap.get(c.getXz())==null){
                        List<String> list1 = new ArrayList<>();
                        list1.add(c.getCwh());
                        cwhMap.put(c.getXz(),list1);
                    }else {
                        List<String> list1 = cwhMap.get(c.getXz());
                        if(!checlArrayHave(c.getCwh(),list1)){
                            list1.add(c.getCwh());
                            cwhMap.put(c.getXz(),list1);
                        }
                    }
                }


            }
        }
        for (String  xz: xzNameList){
          Circuit circuit1 = new Circuit();
          circuit1.setXz(xz);
          circuit1.setSxStaDate(circuit.getSxStaDate());
          circuit1.setSxEndDate(circuit.getSxEndDate());
          CurcuitTj curcuitTj = getTongji(circuit1);
          curcuitTjs.add(curcuitTj);
          List<String> cshNames = cwhMap.get(xz);
          if(cshNames.size()>0){
              for (String cwh: cshNames) {
                  Circuit circuit2 = new Circuit();
                  circuit2.setSxEndDate(circuit.getSxEndDate());
                  circuit2.setSxStaDate(circuit.getSxStaDate());
                  circuit2.setCwh(cwh);
                  CurcuitTj curcuitTj1 =getTongji(circuit2);
                  curcuitTjs.add(curcuitTj1);
                 Circuit circuit3 =new Circuit();
                 circuit3.setSxStaDate(circuit2.getSxStaDate());
                 circuit3.setSxEndDate(circuit2.getSxEndDate());
                 circuit3.setCwh(cwh);
                 circuit3.setXz(xz);
                 List<Circuit> circuitList = circuitMapper.sleletongji(circuit3);
                 List<CurcuitTj> curcuitTjList = getTongjiZrc(circuitList);
                 curcuitTjs.addAll(curcuitTjList);
              }

          }
        }
        return  curcuitTjs;

    }

    public  boolean checlArrayHave(String str,List<String> list){
        if (list == null || list.size()==0){
            return  false;
        }
        for (String s :list) {
            if (str.equals(s)){
                return  true;
            }
        }
        return  false;
    }

    public  CurcuitTj getTongji(Circuit circuit){
        CurcuitTj curcuitTj = new CurcuitTj();
        if(circuit.getCwh()!= null&&! "".equals(circuit.getCwh())){
            curcuitTj.setCwh(circuit.getCwh());
        } else {
            curcuitTj.setXz(circuit.getXz());
        }
        List<Circuit> list = circuitMapper.sleletongji(circuit);
        for (Circuit circuit1 : list){
          curcuitTj =  formatTj(curcuitTj,circuit1);
        }
        return curcuitTj;
    }

    private  CurcuitTj formatTj(CurcuitTj curcuitTj,Circuit circuit1){

        if(circuit1.getSewagedisposal()!= null &&! "".equals(circuit1.getSewagedisposal())){
            if(curcuitTj.getDunshu() == null){
                curcuitTj.setDunshu(circuit1.getSewagedisposal());
            }else {
                curcuitTj.setDunshu(String.valueOf(Integer.valueOf(curcuitTj.getDunshu())+Integer.valueOf(circuit1.getSewagedisposal())));
            }
        }
        if(circuit1.getXcrq() != null){
            if(curcuitTj.getXccs() == null){
                curcuitTj.setXccs("1");
            }else {
                curcuitTj.setXccs(String.valueOf(Integer.valueOf(curcuitTj.getXccs())+1));
            }
        }
        if(circuit1.getSbwxrq()!=null){
            if(curcuitTj.getWxcs() == null){
                curcuitTj.setWxcs("1");
            } else {
                curcuitTj.setWxcs(String.valueOf(Integer.valueOf(curcuitTj.getWxcs())+1));
            }
        }
        if(circuit1.getGwxcqk()!= null && !"".equals(circuit1.getGwxcqk())){
            if(curcuitTj.getGwxccs() == null){
                curcuitTj.setGwxccs("1");
            } else {
                curcuitTj.setGwxccs(String.valueOf(Integer.valueOf(curcuitTj.getGwxccs())+1));
            }
        }
        return  curcuitTj;
    }

    private  List<CurcuitTj> getTongjiZrc(List<Circuit> list){
        List<String> name = new ArrayList<>();
        Map<String,CurcuitTj> map = new HashMap<>();
        for (Circuit circuit :list){
            if(!checlArrayHave(circuit.getZrc(),name)){
                name.add(circuit.getZrc());
                map.put(circuit.getZrc(),new CurcuitTj());
            }
        }
       for(Circuit circuit : list){
        CurcuitTj curcuitTj = formatTj(map.get(circuit.getZrc()),circuit);
        curcuitTj.setZrc(circuit.getZrc());
        curcuitTj.setXz(null);
        curcuitTj.setCwh(null);
        map.put(circuit.getZrc(),curcuitTj);
       }
       List<CurcuitTj> curcuitTjList = new ArrayList<>();
        for (String s: name) {
          curcuitTjList.add(map.get(s));
        }
        return  curcuitTjList;
    }
}

